﻿namespace ScrumLite.WebUx.Tests
{
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using ScrumLite.Services;
    using ScrumLite.WebUx.Controllers;
    using ScrumLite.WebUx.Models;
    using System.Security.Permissions;
    using System.Web.Mvc;

    /// <summary>
    /// Summary description for PeopleControllerFixture
    /// </summary>
    [TestClass]
    public class PeopleControllerFixture
    {
        [TestMethod]
        public void ShouldShowPeopleWithAbilitiesAndEducationLinks()
        {
            MockProfileService profileService = new MockProfileService();
            MockPeopleService peopleService = new MockPeopleService();
            TestablePeopleController controller = new TestablePeopleController(profileService, peopleService);

            controller.List();

            Assert.IsTrue(profileService.GetPeopleCalled);
            Assert.IsNotNull(controller.DataToShow);
            Assert.IsInstanceOfType(controller.DataToShow, typeof(PeopleListViewData));
            Assert.AreEqual(5, ((PeopleListViewData)controller.DataToShow).People.Count);
        }

        [TestMethod]
        public void ShouldControllerPassPersonEducationViewDataToEducationView()
        {
            MockProfileService profileService = new MockProfileService();
            MockPeopleService peopleService = new MockPeopleService();
            TestablePeopleController controller = new TestablePeopleController(profileService, peopleService);

            MockHelpers.SetupMocks(controller);

            controller.Education("southy");

            Assert.IsNotNull(controller.DataToShow);
            Assert.IsInstanceOfType(controller.DataToShow, typeof(PersonEducationViewData));
            Assert.AreEqual("southy", ((PersonEducationViewData)controller.DataToShow).Person.Name);
            Assert.IsTrue(((PersonEducationViewData)controller.DataToShow).Education.Count > 0);
        }

        [TestMethod]
        public void ShouldControllerPassPersonAbilitiesViewDataToAbilitiesView()
        {
            MockProfileService profileService = new MockProfileService();
            MockPeopleService peopleService = new MockPeopleService();
            TestablePeopleController controller = new TestablePeopleController(profileService, peopleService);

            MockHelpers.SetupMocks(controller);

            controller.Abilities("southy");

            Assert.IsNotNull(controller.DataToShow);
            Assert.IsInstanceOfType(controller.DataToShow, typeof(PersonAbilitiesViewData));
            Assert.AreEqual("southy", ((PersonAbilitiesViewData)controller.DataToShow).Person.Name);
            Assert.IsTrue(((PersonAbilitiesViewData)controller.DataToShow).Abilities.Count > 0);
        }
    }

    public class TestablePeopleController : PeopleController
    {
        public object DataToShow { get; set; }
        public object DataRedirectToAction { get; set; }

        public TestablePeopleController(IProfileService profile, IPeopleService people): 
            base(profile, people)
        {
        }
        
        [SecurityPermission(SecurityAction.LinkDemand, Unrestricted = true)]
        protected override void RenderView(string viewName, string masterName, object viewData)
        {
            DataToShow = viewData;
        }
        protected override void RedirectToAction(object values)
        {
            DataRedirectToAction = values;
        }
    }
}
